// pages/personal/applyMerchants/applyMerchants.js
const app = getApp();
const api = require('../../../utils/api');
var apis = api.api;
// 引入腾讯位置服务
var QQMapWX = require('../../../lib/qqmap-wx-jssdk.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
	key: 'PBOBZ-RG6CD-P5A4M-PTTN4-BMO62-SGFAZ'
});
const chooseLocation = requirePlugin('chooseLocation');
//定时获取用户的经纬度
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		name: '',
		shopName: '',
		shopAddress: '',
		fileList: [
			// Uploader 根据文件后缀来判断是否为图片文件
			// 如果图片 URL 中不包含类型信息，可以添加 isImage 标记来声明
		],
		pic: '',
		show: false, //是否显示弹框
	},
	nameInp(e) {
		this.setData({
			name: e.detail
		})
	},
	shopNameInp(e) {
		this.setData({
			shopName: e.detail
		})
	},
	shopAddressInp(e) {
		this.setData({
			shopAddress: e.detail
		})
	},
	subApply() {
		// 提交申请
		// https://dageda.api.hjdz.ltd/userdjorder/applyTrader
		var that = this;
		if (that.data.shopAddress !== '' && that.data.shopName !== '' && that.data.name !== '' && that.data
			.pic !== '') {
			let url = apis.host + 'userdjorder/applyTrader';
			qqmapsdk = new QQMapWX({
				key: 'PBOBZ-RG6CD-P5A4M-PTTN4-BMO62-SGFAZ'
			});
			qqmapsdk.geocoder({
				address: that.data.shopAddress,
				success: function(res) {
					var res = res.result;
					var latitude = res.location.lat;
					var longitude = res.location.lng;
					var data = {
						realname: that.data.name,
						/* 是	string	真实姓名 */
						title: that.data.shopName,
						/* 是	string	商家名 */
						address: that.data.shopAddress,
						/* 是	string	商家地址 */
						yingyeCard: that.data.pic,
						/* 是	string	营业执照 */
						lnglat: longitude + ',' + latitude,
						/* 是	string	经纬度 */
						yw_id: app.getYwId()
					}
					apis.request(url, data, 'POST').then(function(res) {
						if (res.code == 200) {
							wx.showToast({
								title: res.msg,
								success: function() {
									that.setData({
										show: true
									})
								}
							})
						}
					})
				},
				fail: function(res) {
					var location = wx.getStorageSync('nowLocat')
					var data = {
						realname: that.data.name,
						/* 是	string	真实姓名 */
						title: that.data.shopName,
						/* 是	string	商家名 */
						address: that.data.shopAddress,
						/* 是	string	商家地址 */
						yingyeCard: that.data.pic,
						/* 是	string	营业执照 */
						lnglat: location.lng + ',' + location.lat,
						/* 是	string	经纬度 */
						yw_id: app.getYwId()
					}
					apis.request(url, data, 'POST').then(function(res) {
						if (res.code == 200) {
							wx.showToast({
								title: res.msg,
								success: function() {
									that.setData({
										show: true
									})
								}
							})
						}
					})
				}
			})
		} else {
			wx.showToast({
				title: '请把信息填完！',
				icon: 'none'
			})
		}
	},
	afterRead(event) {
		var that = this;
		// 图片上传
		const {
			file
		} = event.detail;
		app.uploadCommonImgAdmin([event.detail.file.url], function(res) {
			const fileList = [];
			fileList.push({
				...file,
				url: res.data.path
			});
			that.setData({
				fileList: fileList,
				pic: res.data.path
			})
		})
	},
	delete(e) {
		// 删除图片
		this.data.fileList.splice([e.detail.index], 1)
		this.setData({
			fileList: this.data.fileList
		});
		let img_list = []
		this.data.fileList.forEach(element => {
			img_list.push(element.url)
		});
		// 删除后的图片传给父组件，父组件setData赋值数据
		this.triggerEvent('dalete', img_list);
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	getData() {
		// 获取是否有申请过资质
		// https://dageda.api.hjdz.ltd/userdjorder/checkApply
		var that = this;
		let url = apis.host + 'userdjorder/checkApply';
		let data = {};
		let user = wx.getStorageSync('isLogin');
		if (!user) {
			//未登录状态
			wx.navigateTo({
				url: '/pages/login/login'
			})
			return false;
		}
		apis.request(url, data, 'POST').then(function(res) {
			if (res.code == 200) {
				wx.redirectTo({
					url: '/pages/personal/personal'
				})
			} else if (res.code == 197) {
				that.setData({
					show: false
				})
			} else {
				that.setData({
					show: true
				})
			}
		}).catch(function(res) {
			that.setData({
				show: false,
			})
		})
	},
	onLoad: function(options) {

	},
	onShow() {
		this.getData()
	}
})
